package com.mrd.shiptask_shipowner.activity;

import java.util.ArrayList;
import java.util.List;

import com.mrd.shiptask_shipowner.R;
import com.mrd.shiptask_shipowner.constants.database.DBUtil;
import com.mrd.shiptask_shipowner.fragments.ElectronNotcompletedFragment;
import com.mrd.shiptask_shipowner.fragments.ElectroncompletedFragment;
import com.mrd.shiptask_shipowner.fragments.FragmentAdapter;
import com.mrd.shiptask_shipowner.unit_tools.FunctionOfActivity;
import com.mrd.shiptask_shipowner.unit_tools.SysApplication;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class ElectronManifestListActivity extends FragmentActivity {
	private final ElectronManifestListActivity theAc = ElectronManifestListActivity.this;

	/**
	 * 左右滑动
	 */
	private TextView completedTap;
	private TextView notCompletedTap;
	/**
	 * 屏幕的宽度
	 */
	private Button backToBeforeBt;
	private ViewPager mViewPager;
	private FragmentAdapter mAdapter;
	private final List<Fragment> fragments = new ArrayList<Fragment>();

	private Resources res;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.electron_mainifest_list_main);
		initData();
		SysApplication.getInstance().addActivity(this);
	}

	private void initData() {
		baseInitView();
		/**
		 * 页面滑动部分
		 */
		res = getResources();
		initView();

		/**
		 * 初始化Adapter
		 */
		mAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);

		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(new TabOnPageChangeListener());
		// 如果未完成列表为空，则自动跳到已完成列表
		if (DBUtil.selectElectronTable(theAc, false).length() <= 0) {
			mViewPager.setCurrentItem(1);
		}
		initTabLine();
		// 版本检查
	}

	/**
	 * 根据屏幕的宽度，初始化引导线的宽度
	 */
	private void initTabLine() {

		// 获取屏幕的宽度
		DisplayMetrics outMetrics = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay()
				.getMetrics(outMetrics);

	}

	/**
	 * 初始化控件，初始化Fragment
	 */
	private void initView() {
		TextView topTitleTv = (TextView) findViewById(R.id.topTitleTv);
		topTitleTv.setText("运输合同列表");
		completedTap = (TextView) findViewById(R.id.completedTap);
		notCompletedTap = (TextView) findViewById(R.id.notCompletedTap);
		notCompletedTap.setOnClickListener(new TabOnClickListener(0));
		completedTap.setOnClickListener(new TabOnClickListener(1));

		fragments.add(new ElectronNotcompletedFragment());
		fragments.add(new ElectroncompletedFragment());
		backToBeforeBt = (Button) findViewById(R.id.backToBeforeBt4);
		backToBeforeBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				FunctionOfActivity.progressDialogShow(theAc);
				startActivity(new Intent(ElectronManifestListActivity.this,
						MainActivity.class));
			}
		});
	}

	/**
	 * 功能：点击主页TAB事件
	 */
	public class TabOnClickListener implements View.OnClickListener {
		private int index = 0;

		public TabOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mViewPager.setCurrentItem(index);// 选择某一页
		}

	}

	/**
	 * 功能：Fragment页面改变事件
	 */
	public class TabOnPageChangeListener implements OnPageChangeListener {

		// 当滑动状态改变时调用
		@Override
		public void onPageScrollStateChanged(int state) {

		}

		// 当前页面被滑动时调用
		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		// 当新的页面被选中时调用
		@Override
		public void onPageSelected(int position) {
			// 重置所有TextView的字体颜色
			switch (position) {
			case 0:
				notCompletedTap.setTextColor(res.getColor(R.color.white));
				completedTap.setTextColor(res.getColor(R.color.gray));
				notCompletedTap.setBackgroundColor(res.getColor(R.color.blue));
				completedTap.setBackgroundColor(res
						.getColor(R.color.light_gray));
				break;
			case 1:
				completedTap.setTextColor(res.getColor(R.color.white));
				notCompletedTap.setTextColor(res.getColor(R.color.gray));
				notCompletedTap.setBackgroundColor(res
						.getColor(R.color.light_gray));
				completedTap.setBackgroundColor(res.getColor(R.color.blue));
				break;
			}
		}
	}

	private void baseInitView() {
		mViewPager = (ViewPager) findViewById(R.id.pager);
		// 触碰到该区域默认左菜单消失
		mViewPager.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				return false;
			}
		});
	}

	// 在需要监听的activity中重写onKeyDown()。返回键
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			FunctionOfActivity
					.progressDialogShow(ElectronManifestListActivity.this);
			startActivity(new Intent(theAc, MainActivity.class));
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
